<template>
  <div class="login">
    <el-card>
      <img class="img" src="@/assets/logo.png" alt="" />
      <!-- 表单
      el-form  
        label-width:标题宽度
        model:绑定表单数据
        rules:绑定规则
        ref:用于调用它的方法
         el-form-item
             prop:用于表单验证的字段设置
             label-width:用于设置标题宽度

     基本表单验证
         1：el-form绑定model,rules
         2:el-form-item  prop
         3:表单元素绑定v-model
         4:写验证规则
            rules:{
              prop的值:[
              {required:true,message:"错误信息",trigger:'change/blur'},
              {min:最小长度,max:最大长度,message:"错误信息",trigger:'change/blur'},
              {validator:(rule,value,callback)=>{
               成功: callbak()
               失败： callback(new Error(错误信息))
              }}
              ]
            }
     全局表单验证
        1:el-form  ref="form"
        2:this.$refs.form.validate((result)=>{
          result:true验证通过
          false:验证失败
        })
      局部表单验证
      1:el-form  ref="form"
      2:this.$refs.form.validateField(['prop的值'],(errorMessage)=>{
        errorMessage:错误信息
        有错误：验证失败
        为空：验证成功
      })

       -->
      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item prop="mobile">
          <el-input placeholder="请输入手机号" v-model="form.mobile"></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-input placeholder="请输入验证码" v-model="form.code"></el-input>
        </el-form-item>
        <el-form-item prop="isPass">
          <el-checkbox v-model="form.isPass"
            >我已阅读并同意「用户协议」和「隐私条款」</el-checkbox
          >
        </el-form-item>
        <el-form-item>
          <el-button class="btn" type="primary" @click="submit">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>
<script>
import { authorizationsPost } from '@/api/index.js'
import { setItem } from '@/utils/auth.js'
export default {
  data () {
    return {
      form: {
        mobile: '13200000000', //	string	必须		手机号
        code: '246810', //	string	必须		短信验证码
        isPass: false
      },
      rules: {
        mobile: [
          { required: true, message: '必填', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              const reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              if (reg.test(value)) {
                callback()
              } else {
                callback(new Error('请正确输入手机号'))
              }
            },
            trigger: 'blur'
          }
        ], //	string	必须		手机号
        code: [
          { required: true, message: '必填', trigger: 'blur' },
          {
            min: 6,
            max: 6,
            message: '请输入六位验证码',
            trigger: 'blur'
          }
        ], //	string	必须		短信验证码
        isPass: [
          { required: true, message: '必填', trigger: 'change' },
          {
            validator: (rule, value, callback) => {
              if (value) {
                callback()
              } else {
                callback(new Error('请勾选协议'))
              }
            },
            trigger: 'change'
          }
        ]
      }
    }
  },
  created () {},
  methods: {
    submit () {
      // console.log('token:', getItem())
      this.$refs.form.validate(async result => {
        if (result) {
          // 登陆接口调用
          const res = await authorizationsPost(this.form)
          console.log('await登陆接口返回值,', res)
          // 存储token
          setItem(res.data.token)
          this.$message.success('登陆成功')
          // 跳转到首页  a:创建首页所对应组件  b:配制路由  c:跳转
          // this.$route(获取当前路由信息)与this.$router(整 个路由的管理者)
          this.$router.push('/home')
          // localStorage.setItem('token_75', res.data.token)
          // authorizationsPost(this.form).then(res => {
          //   console.log('登陆接口返回值,', res)
          // })
          // this.$message.success('验证成功')
        } else {
          // this.$message.error('验证失败')
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
@import url(~@/styles/login.less);
</style>
